var express = require('express');
var router = express.Router();
var sql = require('../db/sql');
// 获取用户信息
router.post('/getUser', (req, res, next) => {
	var userInfo = req.body;
	sql('school2.1',`select * from student where sno='${userInfo.username}'`).then(data => {
	  if (data.length > 0) {
		res.send({
			code: 200,
			msg: data
		})
	  } else {
		res.send({
		  code: 404,
		  msg: '用户不存在'
		})
	  }
	})
  });


// 登录 开始
router.post('/login', (req, res, next) => {
  var userInfo = req.body;
  sql('school2.1',`select * from student where sno='${userInfo.username}'`).then(data => {
    if (data.length > 0) {
      if (data[0].pwd !== userInfo.password) {
        res.send({
          code: 401,
          msg: '密码错误，请重新输入密码',
        })
      } else {
		  res.send({
			code: 200,
			msg: 'student'
		  })
      }
    } else {
		sql('school2.1',`select * from teacher where tno='${userInfo.username}'`).then(teacherData => {
			if (teacherData.length > 0) {
			  if (teacherData[0].pwd !== userInfo.password) {
				res.send({
				  code: 401,
				  msg: '密码错误，请重新输入密码'
				})
			  } else {
				  res.send({
					code: 200,
					msg: 'teacher'
				  })
			  }
			} else {
			  res.send({
				code: 404,
				msg: '登录失败，请注册账号'
			  })
			}
		})
    }
  })
  
});
// 登录 结束

// 用户注册 开始
router.post('/register', (req, res, next) => {
	let info = req.body;
	// 先查询再写入
	sql('school2.1','select * from user').then(data => {
	  var userOn = data.some(item => {
		return item.username == info.username
	  })
	  if (userOn) {
		res.send({
		  code: 404,
		  msg: '账户已存在,请直接登录'
		})
	  } else {
		sql('school2.1',`insert into user values(
		  '${info.username}',
		  '${info.password}'
		  )`).then(success => {
		  console.log(success);
		  if (success) {
			res.send({
			  code: 200,
			  msg: '注册成功'
			})
		  }
		})
	  }
	})
  })
// 用户注册 结束

// 学生信息录入 开始
router.post('/addStudent', (req, res, next) => {
	let info = req.body;
	sql('school2.1',`insert into student values('${info.sno}','${info.pwd}','${info.name}','${info.sex}','${info.nation}','${info.political}','${info.region}','${info.midSchool}','${info.canNum}','${info.fraction}','${info.category}','${info.tno}','${info.hno}')`)
	 .then(success => {
	  if (success) {
	    res.send({
	      code: 200,
	      msg: '信息录入成功'
	    })
	  }
	})
})
// 学生信息录入 结束

// 修改密码 开始
router.post('/forget', (req, res, next) => {
	let info = req.body;
	sql('school2.1',`update user set password = '${info.password}' where username = '${info.username}'`)
	 .then(success => {
	  if (success) {
	    res.send({
	      code: 200,
	      msg: '信息录入成功'
	    })
	  }
	})
})
// 修改密码 结束

// 请假 开始
router.post('/leave', (req, res, next) => {
	let info = req.body;
	sql('school2.1',`insert into leaveDate values( '${info.types}','${info.startDate}','${info.endDate}','${info.student}','${info.teacher}','${info.reason}')`)
	 .then(success => {
	  if (success) {
	    res.send({
	      code: 200,
	      msg: '信息录入成功'
	    })
	  }
	})
})
// 请假 结束

// 报修 开始
router.post('/repair', (req, res, next) => {
	let info = req.body;
	sql('school2.1',`insert into repair values( '${info.project}','${info.campus}','${info.building}','${info.houseNumber}','${info.contact}','${info.startDate}','${info.matters}')`)
	 .then(success => {
	  if (success) {
	    res.send({
	      code: 200,
	      msg: '信息录入成功'
	    })
	  }
	})
})
// 报修 结束

// 学生信息 开始
router.post('/stuMsg', (req, res, next) => {
	let info = req.body;
	sql('school2.1',`insert into stuMsg values( '${info.canNum}','${info.college}','${info.major}','${info.class}','${info.education}')`)
	 .then(success => {
	  if (success) {
	    res.send({
	      code: 200,
	      msg: '信息录入成功'
	    })
	  }
	})
})
// 学生信息 结束






// 页面渲染数据  开始
router.get('/getStudent', (req, res, next) => {
	let info = req.body;
	sql('school2.1',`select * from student where canNum = '${info.canNum}'`).then(data=>{
		if(data){
			res.send({
				code:200,
				data:data
			});
		}else{
			res.send({
				code:500,
				msg: '查询数据失败'
			});
		}
	})
});
router.get('/grade', (req, res, next) => {
	sql('school2.1',`select * from grade`).then(data=>{
		if(data){
			res.send({
				code:200,
				data:data
			});
		}else{
			res.send({
				code:500,
				msg: '查询数据失败'
			});
		}
	})
});

router.get('/news', (req, res, next) => {
	sql('school2.1',`select * from news`).then(data=>{
		if(data){
			res.send({
				code:200,
				data:data
			});
		}else{
			res.send({
				code:500,
				msg: '查询数据失败'
			});
		}
	})
});

router.get('/schoolNews', (req, res, next) => {
	sql('school2.1',`select * from schoolNews`).then(data=>{
		if(data){
			res.send({
				code:200,
				data:data
			});
		}else{
			res.send({
				code:500,
				msg: '查询数据失败'
			});
		}
	})
});

router.get('/notice', (req, res, next) => {
	sql('school2.1',`select * from notice`).then(data=>{
		if(data){
			res.send({
				code:200,
				data:data
			});
		}else{
			res.send({
				code:500,
				msg: '查询数据失败'
			});
		}
	})
});

router.get('/social', (req, res, next) => {
	sql('school2.1',`select * from social`).then(data=>{
		if(data){
			res.send({
				code:200,
				data:data
			});
		}else{
			res.send({
				code:500,
				msg: '查询数据失败'
			});
		}
	})
});

router.post('/details', (req, res, next) => {
	var userInfo = req.body;
	sql('school2.1',`select * from details where num='${userInfo.num}'`).then(data => {
	  if (data.length > 0) {
		res.send({
			code: 200,
			msg: data
		})
	  } else {
		res.send({
		  code: 404,
		  msg: '查询数据失败'
		})
	  }
	})
});

router.get('/query', (req, res, next) => {
	sql('school2.1',`select * from query`).then(data=>{
		if(data){
			res.send({
				code:200,
				data:data
			});
		}else{
			res.send({
				code:500,
				msg: '查询数据失败'
			});
		}
	})
});

router.get('/study', (req, res, next) => {
	sql('school2.1',`select * from study`).then(data=>{
		if(data){
			res.send({
				code:200,
				data:data
			});
		}else{
			res.send({
				code:500,
				msg: '查询数据失败'
			});
		}
	})
});
// 页面渲染数据  结束


module.exports = router;
